<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/28
 * Time: 10:06
 */
namespace app\blog\model;
use think\Model;
use think\Db;
class ShopModel extends Model
{
    /**
     * 获取店铺列表
     */
    public function index($params){
        $where = array();
        $id = cmf_get_current_admin_id();
        if($id != 1){
            $ret = Db::name('user')->where(['id'=>$id])->find();
            $where['s.agency_id'] = $ret['user_guid'];
        }
        if (isset($params['shop_name'])) {
            $where['shop_name'] = ['like', "%{$params['shop_name']}%"];
        }
        $result = $this->alias('s')
                        ->join('__AGENCY__ a','s.agency_id=a.user_id','left')
                        ->join('__SHOP_TYPE__ st','s.stype_id=st.st_guid')
                        ->where($where)
                        ->field('s.*,a.brand_name,st.type_name')
                        ->paginate(15);
        return $result;
    }
    /**
     * 添加店铺
     * $data 店铺参数
     * $code 激活码
     */
    public function addPost($data){
        //开启事物
        $result = $this->insert($data);
        if($result !=false){
            return 1;
        }else{
            return 2;
        }

    }
    /**
     * 根据店铺名称判断店铺是否存在
    */
    public function contrast($name){
        $result = $this->where(['shop_name'=>$name])->find();
        return$result;
    }
    /**
     * 根据店铺id获取指定店铺信息
     */
    public function shopOne($id){
        $result = $this->where(['re_guid'=>$id])->find();
        return $result;
    }
    /**
     * 根据代理商id获取代理商下的没有店长的店铺
     */
    public function getNoshop($id){
        $result = $this->where(['user_id'=>0,'agency_id'=>$id])->select();
        return $result;
    }
	//判断店铺状态 未激活
    public function ShopBan($id){
        $result = Db::name("shop")->where(["re_guid" => $id])->setField('status', '2');
        return $result;
    }
    //判断店铺状态 未激活
    public function ShopCancelBan($id){
        $result = Db::name("shop")->where(["re_guid" => $id])->setField('status', '1');
        return $result;
    }
    /**
     * 获取我的店铺信息（适用与代理商和店长）
     */
    public function ownShop(){
        $id = cmf_get_current_admin_id();
        if($id != 1){
            $res = Db::name('user')->where(['id'=>$id])->find();
            $rest = $this->alias('s')
                            ->join('__USER__ u','s.re_guid=u.shop_guid')
                            ->join('__AGENCY__ a','s.agency_id=a.user_id')
                            ->join('__SHOP_TYPE__ st','s.stype_id=st.st_guid')
                            ->where(['s.user_id'=>$res['user_guid']])
                            ->field('s.*,u.mobile,a.brand_name,st.type_name')
                            ->find();
            return $rest;
        }else{
            return $rest = '';
        }
    }
    /**
     * 编辑我的店铺
     */
    public function ownPost($param){
        $id = $param['id'];
        $date = array();
        $date['shop_name'] = $param['shop_name'];
        $date['address']   = $param['address'];
        $rest = $this->where(['re_guid'=>$id])->update($date);
        return $rest;
    }
}